package me.ratel.demo.eBook.interfaces.booking.web;

import com.alibaba.fastjson.JSON;
import me.ratel.demo.eBook.application.IOrderingService;
import me.ratel.demo.eBook.application.IUserService;
import me.ratel.demo.eBook.infrastructure.exceptions.BusinessException;
import me.ratel.demo.eBook.interfaces.utils.ResponseUtil;
import me.ratel.demo.eBook.interfaces.web.WebConstants;
import me.ratel.demo.eBook.interfaces.web.WebError;
import me.ratel.demo.wechat.entities.BrandWCPayRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 * @time 2016/7/3 21:45
 */
@Controller
public class BookingController {
    private static final Logger logger = LoggerFactory.getLogger(BookingController.class);

    @Autowired private IUserService userService;

    @Autowired private IOrderingService orderingService;

    @RequestMapping("/book/index") public String index() {
        return "book/index";
    }

    @RequestMapping("/book/detail") public ModelAndView bookDetail(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        try {
            String state = request.getParameter("state");

            String code = request.getParameter("code");

            logger.info("/user/oauth2/authorize 参数 [{}] [{}]", state, code);

            model.addAttribute("userId", userService.authorizeOAuth2User(code, state));
        } catch (Throwable t) {
            logger.error("",t);
        }
        return new ModelAndView("book/detail", model);
    }

    @RequestMapping("/prepayId/generate") public void generatePrepayId(HttpServletRequest request, HttpServletResponse response) {
        WebError webError;
        try {
            Integer userId = Integer.parseInt(request.getParameter("userId"));

            Integer price = Integer.parseInt(request.getParameter("price"));

            Integer bookId = Integer.parseInt(request.getParameter("bookId"));

            logger.info("/prepayId/generate 参数 [{}] [{}] [{}]", userId, bookId, price);

            BrandWCPayRequest brandWCPayRequest = orderingService.generatePrepayId(userId, bookId, price);

            webError = new WebError(WebConstants.ErrorHeader.SUCCESS, brandWCPayRequest);
        } catch (BusinessException bize) {
            logger.info("/prepayId/generate 业务异常", bize);
            webError = new WebError(bize.getIError());
        } catch (Throwable t) {
            logger.info("/prepayId/generate 系统异常", t);
            webError = new WebError(WebConstants.ErrorHeader.SYS_ERROR, null);
        }
        logger.info("/prepayId/generate 返回 [{}]", JSON.toJSON(webError).toString());

        ResponseUtil.writeJSON(response, webError);
    }

    @RequestMapping("/book/history") public String bookHistory() {
        return "book/history";
    }
}
